<?php

/* * ***********************************************
 * fe - 主配置文件
 * 本地参数相关的参数请放在main-local中
 * ***********************************************
 *
 * @author johnson
 * @since  2018-01-09
 * *************************************
 * ********** */

$params = array_merge(
    require(__DIR__ . '/../../../common/config/params.php'),
    require(__DIR__ . '/params.php')
);

return [
    'id' => 'app-api-v1',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'api\controllers',
    'defaultRoute' => 'store/index',
    'timeZone' => 'Asia/Shanghai',
    'modules' => [
        'app' => [
            'class' => 'api\modules\app\Module',
        ],
        'manage' => [
            'class' => 'api\modules\manage\Module',
        ],
        'openapi' => [
            'class' => 'api\modules\openapi\Module',
        ],
    ],
    'components' => [
        'user' => [
            'identityClass' => 'app\components\UserCenter', // User must implement the IdentityInterface
            'enableAutoLogin' => true,
            // 'loginUrl' => ['user/login'],
            // ...
        ],
        'curl' => [
            'class' => 'api\components\Curl',
        ],
        'userCenter' => [
            'class' => 'api\components\UserCenter',
            'host' => USER_CENTER_HOST,
            'appId' => PAIDAN_SELF_APPID,
            'appKey' => PAIDAN_SELF_APPKEY,
        ],
        'eventCenter' => [ // 案件分类等
            'class' => 'api\components\EventCenter',
            'host' => FlOW_CENTER_HOST,
            'appId' => PAIDAN_SELF_APPID,
            'appKey' => PAIDAN_SELF_APPKEY,
        ],
        'fileServer' => [
            'class' => 'api\components\FileServer',
            'host' => FILE_SERVER,
            'appId' => FILE_SERVER_APPID,
            'appKey' => FILE_SERVER_APPKEY,
            'filePath' => FILE_SERVER_FILE_PATH,
        ],
        'dataCenter' => [
            'class' => 'api\components\DataCenter',
            'host' => DATA_CENTER_HOST,
            'token' => DATA_CENTER_TOKEN,
        ],
        'flowCenter' => [
            'class' => 'api\components\FlowCenter',
            'host' => FlOW_CENTER_HOST,
            'appId' => USER_CENTER_APPID,
            'appKey' => USER_CENTER_APPKEY,
        ],
        'redisCache' => [
            'class' => 'api\components\RedisCache',
        ],
        'snowflake' => [
            'class' => 'api\components\sk\Snowflake',
        ],
        'elasticsearch' => [
            'class' => 'yii\elasticsearch\Connection',
            'nodes' => [
                ['http_address' => PAIDAN_ES_HOST],
            ],
        ],
        'notification' => [
            'class' => 'api\components\Notification',
        ],
        'test_3' => [
            'class' => 'yii\db\Connection',
            'dsn' => PAIDAN_DB_DSN,
            'username' => PAIDAN_DB_USERNAME,
            'password' => PAIDAN_DB_PASSWORD,
            'charset' => 'utf8',
        ],
        'queue' => [
            'class' => yii\queue\db\Queue::class,
            'db' => 'test_3',
            'deleteReleased' => false,
            'mutex' => [
                'class' => yii\mutex\MysqlMutex::class,
                'db' => 'test_3',
            ],
        ],
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => PAIDAN_REDIS_HOST,
            'port' => PAIDAN_REDIS_PORT,
            'database' => PAIDAN_REDIS_DB,
        ],
        'jobRedis' => [ // Job 由 62 上 swoft 执行
            'class' => 'yii\redis\Connection',
            'hostname' => PAIDAN_REDIS_HOST,
            'port' => PAIDAN_REDIS_PORT,
            'database' => PAIDAN_REDIS_JOB_DB,
        ],
        //路由
        'urlManager' => [
            'enablePrettyUrl' => true, //美化url==ture
            'enableStrictParsing' => false, //不启用严格解析
            'showScriptName' => false, //隐藏index.php
            'rules' => [
                // 不需要验证
                'GET api_v1/dispatch-api/form/get-form' => 'app/site/form',
                'POST api_v1/dispatch-api/form/submit' => 'app/site/form-submit',
                'POST api_v1/dispatch-api/site/upload' => 'app/site/upload',

                // todo
                'GET api_v1/<metaLevel1:\d+>/<controller:[-\w]+>s' => 'app/<controller>/index',
                'POST api_v1/<metaLevel1:\d+>/<controller:[-\w]+>s' => 'app/<controller>/create',

                'GET api_v1/<module:\w+>/<controller:[-\w]+>s' => '<module>/<controller>/index',
                'POST api_v1/<module:\w+>/<controller:[-\w]+>s' => '<module>/<controller>/create',
                'GET api_v1/<module:\w+>/<controller:[-\w]+>s/<id:[-\w\d]+>' => '<module>/<controller>/view',
                'DELETE api_v1/<module:\w+>/<controller:[-\w]+>s/<id:[-\w\d]+>' => '<module>/<controller>/delete',
                'PUT api_v1/<module:\w+>/<controller:[-\w]+>s/<id:[-\w\d]+>' => '<module>/<controller>/update',
                'PATCH api_v1/<module:\w+>/<controller:[-\w]+>s/<id:[-\w\d]+>' => '<module>/<controller>/switch',

                'POST api_v1/app/user/login-by-token' => 'app/user/login-by-token',

                'POST api_v1/<module:\w+>/<controller:[-\w]+>/<action:[-\w]+>' => '<module>/<controller>/<action>',
                'GET api_v1/<module:\w+>/<controller:[-\w]+>/<action:[-\w]+>' => '<module>/<controller>/<action>',
            ],
        ],
        'request' => [
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
                'text/json' => 'yii\web\JsonParser',
            ],
        ],
        // 'response' => [
        //     'class' => 'yii\web\Response',
        //     'on beforeSend' => function ($event) {
        //         $response = $event->sender;
        //         if ($response->data !== null) {
        //             $response->data = [
        //                 'success' => $response->isSuccessful,
        //                 'response' => $response->data,
        //             ];
        //             $response->statusCode = 200;
        //         }
        //     },
        // ],
        'log' => [
  'flushInterval' => 1,
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    //'levels' => ['error', 'warning'],
 'levels'=>['error','warning'],
                    'logVars' => [],
                    'logFile' => '@api/runtime/logs/app_error.log' . date('Ymd'),
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
            // 'class' => 'api\components\ExceptionHandler',
        ],
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@api/messages',
                    'sourceLanguage' => 'en-US',
                ],
            ],
        ],
    ],
    'params' => $params,
];
